package com.li.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.li.blog.dao.mapper.TagMapper;
import com.li.blog.dao.pojo.Tag;
import com.li.blog.service.TagService;
import com.li.blog.vo.Result;
import com.li.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
 

  @Autowired
  private TagMapper tagMapper;

  public TagVo copy(Tag tag){
    TagVo tagVo = new TagVo();

    BeanUtils.copyProperties(tag,tagVo);
    return tagVo;
  }
  public List<TagVo> copyList(List<Tag> tagList){
    List<TagVo> tagVoList = new ArrayList<>();
    for (Tag tag : tagList) {
      tagVoList.add(copy(tag));
    }
    return tagVoList;
  }
  @Override
  public List<TagVo> findTagsByArticleId(long articleId) {
    List<Tag> tags = tagMapper.findTagsByArticleId(articleId);
    return copyList(tags);
  }

  @Override
  public Result hots(int limit) {
    List<Long> tags = tagMapper.findHostTagIds(limit);
    if (CollectionUtils.isEmpty(tags)){
      return Result.success(Collections.emptyList());
    }

    List<Tag> tagList =tagMapper.findTagsByTagIds(tags);

    return Result.success(tagList);
  }

  @Override
  public Result findAll() {
    List<Tag> tags = this.tagMapper.selectList(new LambdaQueryWrapper<>());
    return Result.success(copyList(tags));
  }
  @Override
  public Result findDetailById(Long id) {
    Tag tag = tagMapper.selectById(id);
    TagVo copy = copy(tag);
    return Result.success(copy);
  }
  @Override
  public Result findAllDetail() {
    LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
    List<Tag> tags = this.tagMapper.selectList(queryWrapper);
    return Result.success(copyList(tags));
  }


}
